package com.svm.operators;

import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import com.svm.bean.Image;
import com.svm.bean.Parameter;
import com.svm.math.CosineTransform;
import com.svm.math.FourierTransform;
import com.svm.util.SystemConfig;

public class FeatureExtraction {

	Parameter parameter;

	public FeatureExtraction(Parameter parameter) {

		this.parameter = parameter;
	}

	public Image extract() {
		BufferedImage bufferedImage;
		Image image = new Image();
		double[] pixels;

		bufferedImage = this.resize(
				parameter.getImagesFile().getAbsolutePath(),
				SystemConfig.WIDTH, SystemConfig.HEIGHT);

		pixels = this.transform((RenderedImage) bufferedImage);

		StringBuilder stringBuilder = this.transformToFile(pixels);

		File file = this.saveFile(stringBuilder);

		image.setName(file.getName().substring(0, file.getName().length() - 4));
		image.setFeatureFilePath(file.getAbsolutePath());
		image.setPath(parameter.getImagesFile().getAbsolutePath());

		return image;
	}

	private BufferedImage resize(String imagePath, Integer width, Integer height) {
		return ImageResize.resize(imagePath, width, height);
	}

	private double[] transform(RenderedImage image) {

		if (parameter.getCompAlgorithm().equals("FFT")) {
			FourierTransform fourier = new FourierTransform(parameter);
			return fourier.fft(image);
		} else {
			CosineTransform cosine = new CosineTransform(parameter);
			return cosine.dct(image);
		}

	}

	private StringBuilder transformToFile(double[] pixels) {
		StringBuilder stringBuilder = new StringBuilder();

		for (int i = 0; i < pixels.length; i++) {
			stringBuilder.append(i + 1);
			stringBuilder.append(",");
			stringBuilder.append(pixels[i]);
			if (i < pixels.length - 1) {
				stringBuilder.append(",");
			}
		}
		return stringBuilder;
	}

	private File saveFile(StringBuilder stringBuilder) {
		File file = null;
		FileWriter fileWriter = null;
		String name = null;

		try {

			name = parameter
					.getImagesFile()
					.getName()
					.substring(0,
							parameter.getImagesFile().getName().length() - 4);

			if (parameter.getFase() == 'c') {
				file = new File(SystemConfig.FEATURES_IMAGE_REPOSITORY
						+ parameter.getClassName().toLowerCase());
				if (!file.isDirectory()) {
					file.mkdir();
				}
				file = new File(SystemConfig.FEATURES_IMAGE_REPOSITORY
						+ parameter.getClassName().toLowerCase() + "\\" + name
						+ ".fet");
			} else if (parameter.getFase() == 'q') {
				file = new File(SystemConfig.TMP_REPOSITORY
						+ parameter.getClassName().toLowerCase());
				if (!file.isDirectory()) {
					file.mkdir();
				}
				file = new File(SystemConfig.TMP_REPOSITORY
						+ parameter.getClassName().toLowerCase() + "\\" + name
						+ ".fet");
			}

			fileWriter = new FileWriter(file);
			fileWriter.write(stringBuilder.toString());
			fileWriter.close();

		} catch (IOException ex) {
			System.out.println(ex);
		}

		return file;
	}
}
